原生 Javascript Ajax 请求

/**
* javascript request
*
* @param string method (POST,GET,PUT,PATCH,UPDATE,DELETE)
* @param string url
* @param function callback request success callback
*
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
*/
function request(method, url, callback){
var http = null;
if(window.XMLHttpRequest){
http = new XMLHttpRequest();
}else if(window.ActiveXObject){
http = new ActiveXObject("Microsoft.XMLHTTP");
}
http.open(method, url, true);
http.onreadystatechange = function(){
if(http.readyState == 4){
if(http.status == 200){
callback(http.responseText);
}
}
}
http.send();
}
//example
request("GET", "/api/userinfo", function(html){
console.log(html);
});